#include "Sprite.h"

#include "Component.h"

Sprite::Sprite()
: Sprite("DEFAULT_IMG")
{
}

Sprite::Sprite(std::string path)
	: Sprite(path, 0, 0)
{
}

Sprite::Sprite(std::string path, float _posX, float _posY)
: mCenter(0.0f, 0.0f, 0.0f)
//, collider(0)
// , mPosition(_posX, _posY, 0.0f)
, isVisible(true)
{
	LoadTexture(path);
	
	// create the transform
	transform = new Transform(_posX, _posY);

	//RECT imgRect = { 0 };
	//imgRect.right = imgInfos.Width;
	//imgRect.bottom = imgInfos.Height;
	//SetSrcRect(imgRect);

	// mPosition = D3DXVECTOR3(_posX, _posY, 0);
	// D3DXMatrixIdentity(&mRotation);
	// D3DXMatrixIdentity(&mTranslation);

	// scale to avoid the image being upside down
	// D3DXMatrixScaling(&S, 1.0f, -1.0f, 1.0f);
	transform->SetScaleY(-1.f);
}

void Sprite::SetPositionX(float x)
{
	transform->SetPositionX(x);
}
void Sprite::SetPositionY(float y)
{
	transform->SetPositionY(y);
}
void Sprite::SetPosition(float x, float y)
{ 
	transform->SetPosition(x, y);
}
void Sprite::SetCenter(float x, float y)
{
	this->mCenter.x = x;
	this->mCenter.y = y;
	// D3DXMatrixIdentity(&mRotation);
}

Sprite::~Sprite()
{
	ReleaseCOM(mTexture);
}

void Sprite::Update(float dt)
{
	// D3DXMatrixTranslation(&mTranslation, mPosition.x, mPosition.y, 0);
}

void Sprite::SetSrcRect(RECT& srcRect)
{
	this->srcRect.top = srcRect.top;
	this->srcRect.left = srcRect.left;
	this->srcRect.bottom = srcRect.bottom;
	this->srcRect.right = srcRect.right;
}

void Sprite::Draw(ID3DXSprite* sprite, D3DXMATRIX& view, D3DXMATRIX& proj)
{

	D3DXMATRIX translate;
	D3DXMATRIX rotation;
	D3DXMATRIX scale;

	transform->TranslationMatrix(&translate);
	transform->RotationMatrix(&rotation);
	transform->ScaleMatrix(&scale);
	HR(sprite->SetTransform(&(rotation * translate * scale)));
	HR(sprite->Begin(D3DXSPRITE_ALPHABLEND | D3DXSPRITE_OBJECTSPACE | D3DXSPRITE_DONOTMODIFY_RENDERSTATE));
	HR(sprite->Draw(mTexture, &srcRect, &mCenter, 0, D3DCOLOR_XRGB(255, 255, 255)));
	HR(sprite->Flush());
	HR(sprite->End());
}

void Sprite::LoadTexture(std::string path)
{
	HRESULT result;
	result = D3DXCreateTextureFromFileEx(gD3DDevice, path.c_str(), 0, 0, 0, 0,
		D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT,
		D3DX_DEFAULT, 0, &imgInfos, NULL, &mTexture);
	
	RECT imgRect = { 0 };
	imgRect.right = imgInfos.Width;
	imgRect.bottom = imgInfos.Height;

	SetSrcRect(imgRect);
	
}

bool Sprite::SetTexture(const char *szTextureFilePath, D3DCOLOR dwKeyColour)
{
	bool bTexture = true;
	/*
	HRESULT Result;
	if (FAILED(Result = D3DXCreateTextureFromFileEx(m_pDevice, szTextureFilePath, 0, 0, 0, 0,
		D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT,
		D3DX_DEFAULT, dwKeyColour, NULL, NULL, &m_pTexture)))
	{
		bTexture = false;
		std::ofstream DebugFile;
		DebugFile.open("Debug.txt");
		if (DebugFile.is_open())
		{
			DebugFile << "D3DXCreateTextureFromFileEx(" << szTextureFilePath << ") Failed:" << std::endl;
			DebugFile << "  Error = 0x" << (LPVOID)Result << std::endl;
			DebugFile << "  " << DXGetErrorString9(Result) << std::endl;
			DebugFile << "  " << DXGetErrorDescription9(Result) << std::endl;
			DebugFile.close();
		}
	}
	*/
	return bTexture;
	
}